﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
using System.Data;
using DQYT.HTDZ.Common;

namespace SkWeb.FYCS
{
    public partial class TB_DJSKCBD : System.Web.UI.Page
    {
        #region 事件处理程序

        //页面加载程序
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindJH();
                InitData();
                BindQX();
            }
            
        }

        //井号列表选择事件
        protected void lbxJH_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取井号ID
            string ID = this.lbxJH.SelectedValue;
            //获取井号
            string JH = this.lbxJH.SelectedItem.Text;

            hidId.Value = this.lbxJH.SelectedValue.ToString();
            InitData();
            lblMsg.Text = "";
        }

        /// <summary>
        /// 备注：分页事件
        /// 时间：2012年11月6日 23:30:12
        /// 作者：jiejiep
        /// </summary>
        protected virtual void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindJH();
        }

        //添加按钮点击事件
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Save();
        }
        
        //查询按钮点击事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                string where = @"JH like'%" + this.txtName.Text.Trim() + "%'";
                PageInfo pageinfo = new PageInfo();
                pageinfo.PageSize = this.AspNetPager1.PageSize;
                pageinfo.PageIndex = this.AspNetPager1.CurrentPageIndex;
                SkgysjbBLL BLL = new SkgysjbBLL();
                DataTable dt = BLL.GetListByPage(where, pageinfo.PageIndex, pageinfo.PageSize).Tables[0];
                this.lbxJH.DataSource = dt;
                this.lbxJH.DataBind();
                this.AspNetPager1.RecordCount = pageinfo.RecordCount;
            }
            else
            {
                PageInfo pageinfo = new PageInfo();
                pageinfo.PageSize = this.AspNetPager1.PageSize;
                pageinfo.PageIndex = this.AspNetPager1.CurrentPageIndex;
                SkgysjbBLL BLL = new SkgysjbBLL();
                DataTable dt = BLL.GetListByPage("", pageinfo.PageIndex, pageinfo.PageSize).Tables[0];
                this.lbxJH.DataSource = dt;
                this.lbxJH.DataBind();
                this.AspNetPager1.RecordCount = pageinfo.RecordCount;
            }
        }

        //弹型选择事件
        protected void ddlstDX_SelectedIndexChanged(object sender, EventArgs e)
        {
            string DXID = this.ddlstDX.SelectedValue.Trim();
            TB_SKJSBLL bll = new TB_SKJSBLL();
            TB_SKJSInfo model = bll.GetModel(int.Parse(DXID));
            hidJG.Value = model.JG.ToString();
            string XL = model.SK_XM_XL.Trim();
            string QX = this.ddlstDX.SelectedItem.Text;
            XL = XL.Substring(0, 2);
            int L = QX.IndexOf("(");
            int R = QX.IndexOf(")");
            if (L > 0 && R > 0)
            {
                QX = QX.Substring(L + 1, R - L - 1);
            }
            else
            {
                QX = "普通";
            }
            txtSGFS.Text = QX + XL;
            //弹型
            string DX = ddlstDX.SelectedItem.Text;
            int L1 = DX.IndexOf("/");
            if (L1 >= 0 )
            {
                DX = DX.Substring(L1+1);
                int L2 = DX.IndexOf("/");
                if (L2 >= 0)
                {
                    DX = DX.Substring(L2+1);
                    int L3 = QX.IndexOf("(");
                    int R3 = QX.IndexOf(")");
                    if (L3 >= 0 && R3 >= 0)
                    {
                        DX = DX.Substring(L3 + 1, R3 - L3 - 1);
                    }
                }
            }
            else
            {
                DX = "";
            }
            //项目
            txtXM.Text = DX + txtQX.Text + txtFHLX.Text;
        }

        #endregion 事件处理程序

        #region 函数集

        /// <summary>
        /// 绑定枪型
        /// </summary>
        public void BindQX()
        {
            TB_SKJSBLL bll = new TB_SKJSBLL();
            
            DataTable dt = bll.GetList("").Tables[0];
            this.ddlstDX.Items.Add("");
            ddlstDX.DataSource = dt;
            ddlstDX.DataBind();
        }

        /// <summary>
        /// 保存数据函数
        /// </summary>
        public void Save()
        {
            try
            {
                TB_DJSKCBBLL bll = new TB_DJSKCBBLL();
                TB_DJSKCBInfo model = new TB_DJSKCBInfo();
                model.ID = Convert.ToInt32(txtID.Value.Trim());
                model.JH = txtJH.Text.Trim();
                if (!txtSJRQ.Text.Trim().Equals(string.Empty)) model.SJRQ = Convert.ToDateTime(txtSJRQ.Text.Trim());
                model.SGFS = txtSGFS.Text.Trim();
                model.LX = txtLX.Text.Trim();
                if (!txtJS.Text.Trim().Equals(string.Empty)) model.JS = Convert.ToInt32(txtJS.Text.Trim());
                model.XM = txtXM.Text.Trim();
                model.DX = ddlstDX.SelectedItem.Text.Trim();
                model.QX = txtQX.Text.Trim();
                model.ZYLQX = txtZYLQX.Text.Trim();
                model.FHLX = txtFHLX.Text.Trim();
                model.JSLX = txtJSLX.Text.Trim();
                if (!Request.Form["txtDJSR"].Trim().Equals(string.Empty)) model.DJSR = Convert.ToDecimal(Request.Form["txtDJSR"].Trim());
                if (!txtSKHD.Text.Trim().Equals(string.Empty)) model.SKHD = Convert.ToDecimal(txtSKHD.Text.Trim());
                if (!txtDS.Text.Trim().Equals(string.Empty)) model.DS = Convert.ToInt32(txtDS.Text.Trim());
                if (!txtDDJ.Text.Trim().Equals(string.Empty)) model.DDJ = Convert.ToDecimal(txtDDJ.Text.Trim());
                if (!Request.Form["txtDJE"].Trim().Equals(string.Empty)) model.DJE = Convert.ToDecimal(Request.Form["txtDJE"].Trim());
                if (!Request.Form["txtDJESZ"].Trim().Equals(string.Empty)) model.DJESZ = Convert.ToDecimal(Request.Form["txtDJESZ"].Trim());
                if (!txtQC.Text.Trim().Equals(string.Empty)) model.QC = Convert.ToDecimal(txtQC.Text.Trim());
                if (!txtQDJ.Text.Trim().Equals(string.Empty)) model.QDJ = Convert.ToDecimal(txtQDJ.Text.Trim());
                if (!txtQC2.Text.Trim().Equals(string.Empty)) model.QC2 = Convert.ToDecimal(txtQC2.Text.Trim());
                if (!txtQDJ2.Text.Trim().Equals(string.Empty)) model.QDJ2 = Convert.ToDecimal(txtQDJ2.Text.Trim());
                if (!Request.Form["txtQJE"].Trim().Equals(string.Empty)) model.QJE = Convert.ToDecimal(Request.Form["txtQJE"].Trim());
                if (!Request.Form["txtQJESZ"].Trim().Equals(string.Empty)) model.QJESZ = Convert.ToDecimal(Request.Form["txtQJESZ"].Trim());
                if (!txtJCQC.Text.Trim().Equals(string.Empty)) model.JCQC = Convert.ToDecimal(txtJCQC.Text.Trim());
                if (!txtJCQDJ.Text.Trim().Equals(string.Empty)) model.JCQDJ = Convert.ToDecimal(txtJCQDJ.Text.Trim());
                if (!Request.Form["txtJCQJE"].Trim().Equals(string.Empty)) model.JCQJE = Convert.ToDecimal(Request.Form["txtJCQJE"].Trim());
                if (!Request.Form["txtQDHJJE"].Trim().Equals(string.Empty)) model.QDHJJE = Convert.ToDecimal(Request.Form["txtQDHJJE"].Trim());
                if (!Request.Form["txtJCQJESZ"].Trim().Equals(string.Empty)) model.JCQJESZ = Convert.ToDecimal(Request.Form["txtJCQJESZ"].Trim());
                if (!txtBZQCDJXH.Text.Trim().Equals(string.Empty)) model.BZQCDJXH = Convert.ToDecimal(txtBZQCDJXH.Text.Trim());
                if (!txtYBCLDJXH.Text.Trim().Equals(string.Empty)) model.YBCLDJXH = Convert.ToDecimal(txtYBCLDJXH.Text.Trim());
                if (!Request.Form["txtPTYBCL"].Trim().Equals(string.Empty)) model.PTYBCL = Convert.ToDecimal(Request.Form["txtPTYBCL"].Trim());
                if (!Request.Form["txtDJCLCB"].Trim().Equals(string.Empty)) model.DJCLCB = Convert.ToDecimal(Request.Form["txtDJCLCB"].Trim());
                if (!txtGDFY.Text.Trim().Equals(string.Empty)) model.GDFY = Convert.ToDecimal(txtGDFY.Text.Trim());
                if (!txtZJF.Text.Trim().Equals(string.Empty)) model.ZJF = Convert.ToDecimal(txtZJF.Text.Trim());
                if (!txtQTBDFY.Text.Trim().Equals(string.Empty)) model.QTBDFY = Convert.ToDecimal(txtQTBDFY.Text.Trim());
                if (!Request.Form["txtDJCB"].Trim().Equals(string.Empty)) model.DJCB = Convert.ToDecimal(Request.Form["txtDJCB"].Trim());
                if (!Request.Form["txtYY"].Trim().Equals(string.Empty)) model.YY = Convert.ToDecimal(Request.Form["txtYY"].Trim());
                if (!txtJDDS.Text.Trim().Equals(string.Empty)) model.JDDS = Convert.ToDecimal(txtJDDS.Text.Trim());
                if (!txtJDDS1.Text.Trim().Equals(string.Empty)) model.JDDS1 = Convert.ToDecimal(txtJDDS1.Text.Trim());
                if (!txtJD.Text.Trim().Equals(string.Empty)) model.JD = Convert.ToDecimal(txtJD.Text.Trim());
                if (!txtJDSK.Text.Trim().Equals(string.Empty)) model.JDSK = Convert.ToDecimal(txtJDSK.Text.Trim());
                if (!txtSKHD2.Text.Trim().Equals(string.Empty)) model.SKHD2 = Convert.ToDecimal(txtSKHD2.Text.Trim());
                if (!txtCLLTF.Text.Trim().Equals(string.Empty)) model.CLLTF = Convert.ToDecimal(txtCLLTF.Text.Trim());
                string where = @" JH='" + txtJH.Text.Trim() + "'";
                string ID = string.Empty;
                DataSet ds = bll.GetList(where);
                if (ds != null && ds.Tables.Count >= 0 && ds.Tables[0].Rows.Count > 0)
                {
                    ID = ds.Tables[0].Rows[0]["ID"].ToString();
                } 
                
                //修改
                if (!string.IsNullOrEmpty(ID))
                {
                    if (!bll.Update(model).Equals("0"))
                    {
                        lblMsg.Text = "修改成功";
                        InitData();
                    }
                }
                //添加
                else
                {
                    if (!bll.Add(model).Equals("0"))
                    {
                        lblMsg.Text = "添加成功";
                        InitData();
                    }
                }
            }
            
            
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

        //页面加载方法
        public void InitData()
        {
            TB_DJSKCBBLL bll = new TB_DJSKCBBLL();
            TB_DJSKCBInfo model = new TB_DJSKCBInfo();
            SkgysjbBLL skBLL = new SkgysjbBLL();
            SkgysjbModel skModel = new SkgysjbModel();
            string ID = this.hidId.Value.Trim();
            // 获得主键参数
            if (!string.IsNullOrEmpty(ID))
            {
                model = bll.GetModel(int.Parse(ID));
                skModel = skBLL.GetModel(int.Parse(ID));
                txtID.Value = hidId.Value;
                txtJH.Text = this.lbxJH.SelectedItem.Text;
                txtSJRQ.Text = model.SJRQ == Convert.ToDateTime("1900-1-1") ? string.Empty : model.SJRQ.ToString();
                if (!string.IsNullOrEmpty(model.SGFS))
                {
                    txtSGFS.Text = model.SGFS.ToString();
                }
                else
                {
                    txtSGFS.Text = "";
                }
                if (!string.IsNullOrEmpty(model.LX))
                    txtLX.Text = model.LX.ToString();
                else
                    txtLX.Text = skModel.SGLX;
                txtJS.Text = model.JS == int.MinValue ? string.Empty : model.JS.ToString();
                if (!string.IsNullOrEmpty(model.XM))
                    txtXM.Text = model.XM.ToString();
                else
                    txtXM.Text = "";
                if (!string.IsNullOrEmpty(model.DX))
                    ddlstDX.SelectedItem.Text = model.DX.ToString();
                else
                    ddlstDX.SelectedItem.Text = "";
                if (!string.IsNullOrEmpty(model.QX))
                    txtQX.Text = model.QX.ToString();
                else
                    txtQX.Text = skModel.SKQX;
                if (!string.IsNullOrEmpty(model.ZYLQX))
                    txtZYLQX.Text = model.ZYLQX.ToString();
                else
                    txtZYLQX.Text = "";
                if (!string.IsNullOrEmpty(model.FHLX))
                    txtFHLX.Text = model.FHLX.ToString();
                else
                    txtFHLX.Text = skModel.TSSGLX;
                if (!string.IsNullOrEmpty(model.JSLX))
                    txtJSLX.Text = model.JSLX.ToString();
                else
                    txtJSLX.Text = "";
                txtDJSR.Text = model.DJSR == int.MinValue ? string.Empty : model.DJSR.ToString();
                txtSKHD.Text = model.SKHD == int.MinValue ? string.Empty : model.SKHD.ToString();
                txtDS.Text = model.DS == int.MinValue ? string.Empty : model.DS.ToString();
                txtDDJ.Text = model.DDJ == int.MinValue ? string.Empty : model.DDJ.ToString();
                txtDJE.Text = model.DJE == int.MinValue ? string.Empty : model.DJE.ToString();
                txtDJESZ.Text = model.DJESZ == int.MinValue ? string.Empty : model.DJESZ.ToString();
                txtQC.Text = model.QC == int.MinValue ? string.Empty : model.QC.ToString();
                txtQDJ.Text = model.QDJ == int.MinValue ? string.Empty : model.QDJ.ToString();
                txtQC2.Text = model.QC2 == int.MinValue ? string.Empty : model.QC2.ToString();
                txtQDJ2.Text = model.QDJ2 == int.MinValue ? string.Empty : model.QDJ2.ToString();
                txtQJE.Text = model.QJE == int.MinValue ? string.Empty : model.QJE.ToString();
                txtQJESZ.Text = model.QJESZ == int.MinValue ? string.Empty : model.QJESZ.ToString();
                txtJCQC.Text = model.JCQC == int.MinValue ? string.Empty : model.JCQC.ToString();
                txtJCQDJ.Text = model.JCQDJ == int.MinValue ? string.Empty : model.JCQDJ.ToString();
                txtJCQJE.Text = model.JCQJE == int.MinValue ? string.Empty : model.JCQJE.ToString();
                txtQDHJJE.Text = model.QDHJJE == int.MinValue ? string.Empty : model.QDHJJE.ToString();
                txtJCQJESZ.Text = model.JCQJESZ == int.MinValue ? string.Empty : model.JCQJESZ.ToString();
                txtBZQCDJXH.Text = model.BZQCDJXH == int.MinValue ? string.Empty : model.BZQCDJXH.ToString();
                txtYBCLDJXH.Text = model.YBCLDJXH == int.MinValue ? string.Empty : model.YBCLDJXH.ToString();
                txtPTYBCL.Text = model.PTYBCL == int.MinValue ? string.Empty : model.PTYBCL.ToString();
                txtDJCLCB.Text = model.DJCLCB == int.MinValue ? string.Empty : model.DJCLCB.ToString();
                txtGDFY.Text = model.GDFY == int.MinValue ? string.Empty : model.GDFY.ToString();
                txtZJF.Text = model.ZJF == int.MinValue ? string.Empty : model.ZJF.ToString();
                txtQTBDFY.Text = model.QTBDFY == int.MinValue ? string.Empty : model.QTBDFY.ToString();
                txtDJCB.Text = model.DJCB == int.MinValue ? string.Empty : model.DJCB.ToString();
                txtYY.Text = model.YY == int.MinValue ? string.Empty : model.YY.ToString();
                txtJDDS.Text = model.JDDS == int.MinValue ? string.Empty : model.JDDS.ToString();
                txtJDDS1.Text = model.JDDS1 == int.MinValue ? string.Empty : model.JDDS1.ToString();
                txtJD.Text = model.JD == int.MinValue ? string.Empty : model.JD.ToString();
                txtJDSK.Text = model.JDSK == int.MinValue ? string.Empty : model.JDSK.ToString();
                txtSKHD2.Text = model.SKHD2 == int.MinValue ? string.Empty : model.SKHD2.ToString();
                txtCLLTF.Text = model.CLLTF == int.MinValue ? string.Empty : model.CLLTF.ToString();
            }
        }

        /// <summary>
        /// 绑定井号
        /// </summary>
        public void BindJH()
        {
            PageInfo pageinfo = new PageInfo();
            pageinfo.PageSize = this.AspNetPager1.PageSize;
            pageinfo.PageIndex = this.AspNetPager1.CurrentPageIndex;
            SkgysjbBLL BLL = new SkgysjbBLL();
            DataTable dt = BLL.GetListByPage("", pageinfo.PageIndex, pageinfo.PageSize).Tables[0];
            this.lbxJH.DataSource = dt;
            this.lbxJH.DataBind();
            this.AspNetPager1.RecordCount = pageinfo.RecordCount;
        }

        #endregion 函数集 
    }
}